// EvH: [vehicle, position, unit]
// Check info: [roles]
//             [positions] (if not only driver)
private ["_evh","_unit","_vehicle","_position","_positions","_access","_has_roles"];
_evh = _this select 0;
_vehicle = _evh select 0;
_position = _evh select 1;
_unit = _evh select 2;
_roles = [];
if (count _this > 1) then { _roles = _this select 1; };
_positions = ["DRIVER"];
if (count _this > 2) then { _positions = _this select 2; };

if (count _positions <= 0) exitWith {};

_role = assignedVehicleRole _unit;
if (_vehicle isKindOf "Helicopter") then {
	if (count _role > 0) then {
		if (toupper(_role select 0) == "TURRET") then {
			if (count _role > 1) then {
				if (((_role select 1) select 0) == 0 && toupper(_position) == "GUNNER") then {
					_position = "DRIVER";
				};
			};
		};
	};
};
_has_roles = [(getPlayerUID _unit),_roles] call mp_rights_fnc_hasRoles;

_access = switch (true) do {
	case (toupper(_position) in _positions):{
		_has_roles;
	};
	default {
		true;
	};
};

if (debug_mso) then { diag_log format["MSO.%1|MPRIGHTS|CrewCheck|%2|%3|%4|%5",time,_access,_this,_role,_position]; };
// diag_log format["MSO.%1|MPRIGHTS|CrewCheck|%2|%3|%4|%5",time,_access,_this,_role,_position];
if (!_access) then {
	[_vehicle,_unit] call mp_rights_fnc_ejectCrew;
} else {
	// In case the player has not the needed roles and there are restricted positions
	// We control that he doesn't move to the restricted position
	if (!_has_roles && (count _positions) > 0) then {
		[_vehicle,_unit,_positions] spawn {
			private ["_unit","_vehicle","_position","_positions","_role","_is_chopper"];
			_vehicle = _this select 0;
			_unit = _this select 1;
			_positions = _this select 2;
			_is_chopper = _vehicle isKindOf "helicopter";
			while {_vehicle == vehicle player} do {
				_role = assignedVehicleRole _unit;
				if (count _role > 0) then {
					_position = _role select 0;
					if (toupper(_position) == "TURRET") then {
						if (count _role > 1) then {
							if ((((_role select 1) select 0) == 0) && _is_chopper) then {
								_position = "DRIVER";
							} else {
								_position = "GUNNER";
							};
						};
					};
				};
				if (debug_mso) then { diag_log format["MSO.%1|MPRIGHTS|CrewCheck2|%2|%3|%4",time,_this,_role,_position]; };
				if (toupper(_position) in _positions) then {
					[_vehicle,_unit] call mp_rights_fnc_ejectCrew;
				};
				sleep 1.235;
			};
		};
	};
};